package com.flex.reflection.convert;

import com.flex.reflection.MetaClass;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;

/**
 * @author ganlt
 * @since 2019/09/23 13:57
 */
public class FieldIntegerConvert<T> implements FieldConvert {
    private MetaClass.Config config;

    public FieldIntegerConvert(MetaClass.Config config) {
        this.config = config;
    }
    @Override
    public Object convert(Object type) {
        if (type == null) {
            return null;
        }
        Class<?> aClass = type.getClass();
        if (aClass.isAssignableFrom(Date.class)) {
            Date date = (Date) type;
            return date.getTime();
        }

        if (aClass.isAssignableFrom(BigInteger.class)) {
            BigInteger bigInteger = (BigInteger) type;
            return bigInteger.intValue();
        }

        if (aClass.isAssignableFrom(BigDecimal.class)) {
            BigDecimal bigDecimal = (BigDecimal) type;
            return bigDecimal.intValue();
        }

        try {
            return Integer.parseInt(type.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }



}
